/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.zhpan.bannerviewpager.adapter;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;

import com.example.zhpan.bannerviewpager.ResourceTable;
import com.zhpan.bannerview.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * ImageLoader
 *
 * @since 2021-03-24
 */
public class PageProvider extends PageSliderProvider {
    LayoutScatter layoutScatter;
    private ArrayList<Component> components = new ArrayList<>();
    private int[] recourceImage = new int[]{
        ResourceTable.Media_guide0, ResourceTable.Media_guide1, ResourceTable.Media_guide2
    };
    private Context context;

    /**
     * PageProvider
     *
     * @param context 參數
     */
    public PageProvider(Context context) {
        layoutScatter = LayoutScatter.getInstance(context);
        this.context = context;
    }

    /**
     * getCount
     *
     * @return int
     */
    @Override
    public int getCount() {
        return recourceImage.length;
    }

    /**
     * 获取Child 没有
     *
     * @param componentContainer 參數
     * @param position 參數
     * @return Object
     */
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        Component page1 = layoutScatter.parse(ResourceTable.Layout_page_slide_layout, null, false);
        Text tv = (Text) page1.findComponentById(ResourceTable.Id_tv);
        try {
            PixelMapElement pixelMapElement = new PixelMapElement(
                context.getResourceManager().getResource(recourceImage[position]));
            page1.setBackground(pixelMapElement);
            tv.setText("page" + position);
        } catch (IOException e) {
            Log.debug(e.getMessage());
        } catch (NotExistException e) {
            Log.debug(e.getMessage());
        }
        componentContainer.addComponent(page1);
        components.add(position, page1);
        return page1;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i1, Object object) {
        componentContainer.removeComponent((Component) object);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }

    /**
     * getComponent
     *
     * @param index 參數
     * @return components.get(index)
     */
    public Component getComponent(int index) {
        if (index < 0 || index > components.size()) {
            return components.get(index);
        }
        return components.get(index);
    }

    public List<Component> getComponents() {
        return components;
    }
}

